home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2181 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: atol or strtol ?
  5. Date: 19 Jan 1996 14:46:33 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan19094633@g7240065.bridge.bst.bls.com>
  8. References: <4dbobq$763@jupiter.planet.net> <4dk4sc$dmj@spanky.pls.ov.com>
  9.     <ALUN.CHAMPION.96Jan18103414@g7240065.bridge.bst.bls.com>
  10.     <4dn35p$f28@solutions.solon.com>
  11. NNTP-Posting-Host: bstfirewall.bst.bls.com
  12. In-reply-to: seebs@solutions.solon.com's message of 18 Jan 1996 21:28:57 -0600
  13.  
  14. In article <4dn35p$f28@solutions.solon.com> seebs@solutions.solon.com (Peter Seebach) writes:
  15.  
  16. : In article <ALUN.CHAMPION.96Jan18103414@g7240065.bridge.bst.bls.com>,
  17. : Alun Champion <Alun.Champion@bridge.bst.bls.com> wrote:
  18. : >    for (;;) {
  19. : >        num = strtol(ptr, &p, 0);
  20. : >        if (errno && errno != ERANGE) /* We can deal with ERANGE problems. */
  21.  
  22. : Huh?
  23. : What gives you the idea this is correct?
  24.  
  25. strtol() has a hard time dealing with errors as every return from strtol()
  26. is valid.
  27. Mistakenly I refered to my strtol() man pages and it documents it can set errno
  28. on more than ERANGE conditions, as this is meant to ANSI conformant - I didn't
  29. check the standard against what my man page stated.
  30.  
  31. : It is unclear what the semantics are; ANSI asserts that a function may set
  32. : errno whether or not there was an error, *if* the function does not define
  33. : its usage of errno.  However, even if it does, since the standard has not
  34. : seen fit to declare otherwise, we have no guarantee that errno was not
  35. : set, either by strtol, or by some other previous call.
  36.  
  37. : If you set errno to 0 before calling strtol, you are safer, but even then,
  38. : I see no guarantee from the standard that errno cannot be set for reasons
  39. : not mentioned.  (If anyone can verify this, or establish that the library
  40. : may *not* set errno without an error, when it documents conditions for
  41. : setting it, please let me know.)
  42.  
  43. Hmm...
  44.  
  45. : It is almost never correct to check whether or not errno has been set.
  46. : Your current implementation could fail capriciously because some earlier
  47. : call set errno to a value other than ERANGE.
  48.  
  49. Or infact something earlier could have set errno to ERANGE.
  50.  
  51. I can't see how (apart from setting explicitly to 0) you can make the
  52. test for ERANGE (or other conditions) safer. You have to resort to
  53. checking errno, it's a problem with the implementation of strtol().
  54.  
  55. Regards
  56.  
  57.     -A.
  58. -- 
  59. | A.Champion                |
  60.